package com.spring.shiro.client;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spring.shiro.entity.User;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import java.util.List;

public class CxfClient {
    public static void main(String[] args) {

        main1();

    }

    public static void main1() {
        JaxWsDynamicClientFactory jwdcf = JaxWsDynamicClientFactory.newInstance();
        Client client = jwdcf.createClient("http://127.0.0.1:8080/services/cxf?wsdl");

        Object[] objects = null;

        try {
            objects = client.invoke("findUsers");

            String string = JSONArray.toJSONString(objects[0]);
            if (string.contains("[")){
                //字符串转集合[{},{}]
                List<User> users = JSONArray.parseArray(string, User.class);
                System.out.println(users.get(0));
            }else {
                //转成单个对象{k:v,k2:v2}
                User user = JSONObject.parseObject(string, User.class);
                System.out.println(user);
            }

            //返回map操作
//            JSONObject jsonObject = JSONObject.parseObject(string);
//            //拿到Map字符串
//            String entry = jsonObject.getString("entry");
//            //转成List
//            List<HashMap> list = JSONArray.parseArray(entry, HashMap.class);
//            //收集成指定的Map
//            Map<String,Object> map = new HashMap<>();
//            list.forEach(e -> {
//                map.put(String.valueOf(e.get("key")),e.get("value"));
//            });
//            //转成对象
//            User u = JSONObject.parseObject(map.get("zy").toString(),User.class);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
